//
//  MainViewController.swift
//  01-界面搭建
//
//  Created by Apple on 15/11/3.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
     //MARK: - 监听方法
   @objc  private  func clickBtn() {
    print("点击")
    }
      //MARK: - 视图生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        setUpComposedButton()
    }
    override func viewWillAppear(animated: Bool) {
       super.viewWillAppear(animated)
        //移动撰写按钮
       tabBar.bringSubviewToFront(composedButton)
    }
    //MARK: - 懒加载创建按钮控件
    private lazy var composedButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add",
        backgroudName: "tabbar_compose_button")
    
}
//MARK: - 创建子控制器
extension MainViewController {
    ///添加撰写按钮
    private func setUpComposedButton () {
        
        //1.添加按钮
        tabBar.addSubview(composedButton)
        //2.调整按钮
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        composedButton.frame = CGRectInset(tabBar.bounds, 2*w, 0)
        composedButton.addTarget(self, action: "clickBtn", forControlEvents: .TouchUpInside)
    }
    ///添加所有控制器
    private func addChildViewControllers() {
        //设置首页
        addChildViewController(HomeTableViewController(), title: "首页",imageName: "tabbar_home")
        //设置信息
        addChildViewController(MessageTableViewController(), title: "信息",imageName: "tabbar_message_center")
        //设置控制器
        addChildViewController(UIViewController())
        //设置发现
        addChildViewController(DiscoverTableViewController(), title: "发现",imageName: "tabbar_discover")
        //我的设置
        addChildViewController(ProfileTableViewController(), title: "我的设置",imageName: "tabbar_profile")
    }
    //添加控制器
    private  func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        //创建控制器
        let vc  = vc
        //设置标题
        vc.title = title
       
        //设置背景图片
        vc.tabBarItem.image = UIImage(named: imageName)
        //绑定导航控制器
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
}